//
//  ProgressQueryView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2019/1/3.
//  Copyright © 2019年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol ProgressQueryViewDelegate {
    @objc optional func selectpopularBank(urlStr:String) //选择热门银行
}

class ProgressQueryView: UIView {
    
    var title : String! {
        didSet{
            updateData()
        }
    }
    
    fileprivate var urlStr = "" //链接
    
    weak var delegate : ProgressQueryViewDelegate?
    
    fileprivate let cellId = "progressQueryCell"
    
    fileprivate var dataArray = [[String:String]]()
    
    fileprivate var collectionView : UICollectionView!
    
    fileprivate let titleLabel = UILabel()
    fileprivate let tipLabel = UILabel()
    fileprivate let imageView = UIImageView()

    init() {
        super.init(frame: .zero)
        
        NotificationCenter.default.addObserver(self, selector: #selector(getPopularBank(sender:)), name: .popularBankPush, object: nil)
        
        let redView = UIView()
        redView.backgroundColor = UIColor.red
        self.addSubview(redView)
        
        let whiteView2 = UIView()
        whiteView2.backgroundColor = UIColor.white.withAlphaComponent(0.4)
        whiteView2.layer.cornerRadius = RATIO(maxNum: 5)
        whiteView2.layer.masksToBounds = true
        self.addSubview(whiteView2)
        
        let whiteView3 = UIView()
        whiteView3.layer.cornerRadius = RATIO(maxNum: 5)
        whiteView3.layer.masksToBounds = true
        whiteView3.backgroundColor = UIColor.white.withAlphaComponent(0.4)
        self.addSubview(whiteView3)
        
        let whiteView4 = UIView()
        whiteView4.backgroundColor = UIColor.init(red: 255/255.0, green: 180/255.0, blue: 180/255.0, alpha: 1)
        self.addSubview(whiteView4)
        
        let whiteView = UIView()
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = RATIO(maxNum: 5)
        whiteView.layer.masksToBounds = true
        self.addSubview(whiteView)
        
        imageView.image = UIImage.init(named: "jdcx_icon")
        whiteView.addSubview(imageView)
        
        titleLabel.textAlignment = .center
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        whiteView.addSubview(titleLabel)
        
        tipLabel.textAlignment = .center
        tipLabel.backgroundColor = UIColor.lightYellow()
        tipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 12))
        whiteView.addSubview(tipLabel)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = .init(width: ScreenW/4, height: RATIO_H(maxNum: 100))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(BankCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        let grayView = UIView()
        grayView.backgroundColor = UIColor.mineColor()
        self.addSubview(grayView)
        
        redView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 130))
        }
        
        whiteView2.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.left.equalToSuperview().offset(RATIO(maxNum: 60))
            make.right.equalToSuperview().offset(RATIO(maxNum: -60))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        whiteView3.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView2.snp.bottom).offset(RATIO(maxNum: -5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 40))
            make.right.equalToSuperview().offset(RATIO(maxNum: -40))
            make.height.equalTo(whiteView2)
        }
        
        whiteView4.snp.makeConstraints { (make) in
            make.top.height.equalTo(whiteView3)
            make.left.right.equalTo(whiteView2)
        }
        
        whiteView.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView4.snp.bottom).offset(RATIO(maxNum: -5))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 120))
        }
        
        
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.height.greaterThanOrEqualTo(10)
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.left.equalTo(self.snp.centerX).offset(RATIO(maxNum: -35))
        }
        
        imageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(titleLabel)
            make.right.equalTo(titleLabel.snp.left).offset(RATIO(maxNum: -10))
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 40))
            make.right.equalToSuperview().offset(RATIO(maxNum: -40))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 100))
        }
        
        grayView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 5))
        }
        
        //设置阴影
        whiteView.layer.shadowColor = UIColor.black.cgColor
        whiteView.layer.shadowOffset = .init(width: 0, height: 0)
        whiteView.layer.shadowOpacity = 0.2
        whiteView.layer.shadowRadius = 5
        whiteView.clipsToBounds = false
    }
    
    //MARK: 获取热门银行相关数据
    @objc fileprivate func getPopularBank(sender:Notification) {
        
        if let data = sender.userInfo!["data"] as? Data {
            
            var elements:[String]!
            var attributes:[String]!
            var keys:[String]!
            
            switch self.title {
            case progressInquiry://进度查询
                elements = ["//div[@class='all_div_top']/a","//div[@class='all_div_top']/a/img","//div[@class='all_div_top']/a/span"]
                attributes = ["href","src","text"]
                keys = ["bank_href","img_src","bank_text"]
            case creditCardActivation://信用卡激活
                elements = ["//div[@class='all_div_top']/a","//div[@class='all_div_top']/a/img","//div[@class='all_div_top']/a/span"]
                attributes = ["href","src","text"]
                keys = ["bank_href","img_src","bank_text"]
            default:
                break
            }
            
            let array = KMTool.htmlToModel(data: data, elements: elements, attributes: attributes, keys: keys)
            
            for model in array {
                self.dataArray.append(model)
            }
            
            self.collectionView.reloadData()
        }
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self, name: .popularBankPush, object: nil)
    }
    
    //MARK: 更新数据
    fileprivate func updateData() {
        
        
        if title == progressQuery {
            titleLabel.text = progressUpdates
            tipLabel.text = applicationProgress
        }else if title == creditCardActivation {
            titleLabel.text = quickActivation
            tipLabel.text = turnOnYourCard
        }
        
        let width = titleLabel.text?.size(font: RATIO(maxNum: 16), height: RATIO_H(maxNum: 20)).width
        
        titleLabel.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 30))
            make.height.greaterThanOrEqualTo(10)
            make.width.equalTo(width!)
            make.centerX.equalToSuperview().offset(RATIO(maxNum: 15))
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ProgressQueryView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? BankCell
        cell?.titleLabel.text = dataArray[indexPath.row]["bank_text"]
        cell?.headImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: dataArray[indexPath.row]["bank_text"] ?? ""))
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        var urlString  = dataArray[indexPath.row]["bank_href"]
        
        if urlString == "https://kefu.creditcard.cmbc.com.cn/wdxykindex.shtml#dingWeiAllByHSY" {
            
            urlString = "https://kefu.creditcard.cmbc.com.cn/wdxykindex.shtml#loginMD"
        }
        
        self.delegate?.selectpopularBank!(urlStr: urlString ?? "")
    }
    
}
